--
-- Author: shunguo.chen
-- Date: 2017-11-29 10:09:02
--
-- MountQualityView  坐骑品质界面

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.MountQualityScene

local QUALITY_ITEM_STR = L("tips_1990083")
local SOUL_NUM_STR = L("lua_code_text_147")

function ClassRef:init()
    display.setCascadeOpacityEnabled(self._root, true)
    self._ui.Button_cancel:addClickEventListener(function()
        self:closeView()
    end)
    self._ui.Button_up:addClickEventListener(function()
        if not self._itemOk then
            display.pushToast(QUALITY_ITEM_STR)
        else
            self:hide(function()
                executeFunc(self._onOperate, "qualityUp")
                executeFunc(self._onCloseView, self)
            end)
        end
    end)

    local con = self._ui["Panel/Panel_content"]
    local gradePanel = con:getChildByName("Panel_data")
    local curPanel = gradePanel:getChildByName("Panel_1")
    local boundingBox1 = curPanel:getBoundingBox()
    local worldPos = gradePanel:convertToWorldSpace(cc.p(boundingBox1.x, boundingBox1.y))
    boundingBox1.x = worldPos.x
    boundingBox1.y = worldPos.y
    curPanel:addTouchEventListener(function(sender, eventType)
        if not self._mount then return end
        if eventType == ccui.TouchEventType.began then
            UIHandler:hideFloatPanel()
            local mount = self._mount
            local params = {
                tipsType = Enums.TipsType.classSkill,
                dataTemp = mount:getSkill(),
                boundingBox = boundingBox1
            }
            app:dispatchCustomEvent("EVENT_OPEN_FLOAT", {params=params})
        elseif eventType == ccui.TouchEventType.canceled or 
            eventType == ccui.TouchEventType.ended then
            UIHandler:hideFloatPanel()
        end
    end)
    self._panel1 = curPanel

    local nextPanel = gradePanel:getChildByName("Panel_2")
    local boundingBox2 = nextPanel:getBoundingBox()
    local worldPos = gradePanel:convertToWorldSpace(cc.p(boundingBox2.x, boundingBox2.y))
    boundingBox2.x = worldPos.x
    boundingBox2.y = worldPos.y
    nextPanel:addTouchEventListener(function(sender, eventType)
        if not self._mount then return end
        if eventType == ccui.TouchEventType.began then
            UIHandler:hideFloatPanel()
            local mount = self._mount
            local params = {
                tipsType = Enums.TipsType.classSkill,
                dataTemp = mount:getNextSkill(),
                boundingBox = boundingBox2
            }
            app:dispatchCustomEvent("EVENT_OPEN_FLOAT", {params=params})
        elseif eventType == ccui.TouchEventType.canceled or 
            eventType == ccui.TouchEventType.ended then
            UIHandler:hideFloatPanel()
        end
    end)
    self._panel2 = nextPanel

    self._numText = con:findChild("Panel_bar/num")
    self._bar = con:findChild("Panel_bar/LoadingBar")
end

function ClassRef:getMount()
    return self._mount
end

function ClassRef:refresh(mount)
    self._mount = mount
    local quality = mount:getQuality()

    local skills = {mount:getSkill(), mount:getNextSkill()}
    for i,skill in ipairs(skills) do
        local panel = self["_panel"..i]
        local q = quality + i - 1

        local name = panel:getChildByName("name")
        local icon = panel:getChildByName("icon_grade")
        local skillName = panel:getChildByName("Text")
        local skillIcon = panel:findChild("skill_icon/icon")

        name:setString(L(Enums.QualityName[q]))
        name:setColor(Enums.QualityColor[q])
        icon:loadTexture(string.format(Res.MountQualityPath, q))

        skillName:setString(skill:getName())
        skillIcon:loadTexture(skill:getIcon())
    end

    self:refreshMountCollect()
end

function ClassRef:refreshMountCollect()
    local haveNum, needNum = self._mount:getQualityCostNum()
    local per = math.min(haveNum/needNum*100, 100)
    self._numText:setString(string.format(SOUL_NUM_STR, haveNum, needNum))
    self._bar:setPercent(per)

    self._itemOk = (haveNum >= needNum)
end

return ClassRef